<template>
  <div class="login-page">
    <el-card class="box-card">
      <img src="../assets/logo.595745bd.png" alt="" />
      <el-form :rules="rules">
        <el-form-item prop="loginName">
          <el-input
            placeholder="请输入账号"
            prefix-icon="el-icon-mobile-phone"
            v-model="ruleForm.loginName"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            v-model="ruleForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            prefix-icon="el-icon-key"
            placeholder="请输入验证码"
            class="yanzhen"
            v-model="ruleForm.code"
          >
          </el-input>
          <div @click="changeImg()" :v-model="ruleForm.clientToken">
            <img
              :src="`http://likede2-admin.itheima.net/likede//api/user-service/user/imageCode/${this.ruleForm.clientToken}`"
              alt=""
            />
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

export default {
  data () {
    return {
      ruleForm: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        clientToken: '',
        loginType: 0
      },
      rules: {
        loginName: [
          { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
          { pattern: /^\w{5,11}$/, message: '请输入 5 到 10 位的密码', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: ['change'] },
          { pattern: /^\d{4}$/, message: '请输入4位的验证码', trigger: ['blur', 'change'] }
        ]

      }
    }
  },
  async created () {
    await this.changeImg()
  },
  methods: {
    changeImg () {
      this.ruleForm.clientToken = Math.round(Math.random() * 10000)
      // console.log('触发了')
      // this.imgLink = `http://likede2-admin.itheima.net/likede//api/user-service/user/imageCode/:${clientToken}`
      // this.$store.dispatch('user/flagItem', clientToken)
      // console.log(this.ruleForm.clientToken)
    },
    async submitForm () {
      await this.$store.dispatch('user/loginAction', this.ruleForm)
      // console.log(this.$store.state.user.success)
      if (this.$store.state.user.success) {
        this.$router.push('/')
        this.$message({
          message: '登录成功啦',
          type: 'success'
        })
      } else {
        this.$message.error('登录失败啦')
      }
    }
  }

}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-image: url(../assets/login-bgi.png);
  .box-card {
    position: absolute;
    overflow: visible;
    width: 418px;
    height: 288px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    padding: 56px 35px 0;
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
    img {
      position: absolute;
      width: 96px;
      height: 96px;
      top: -46px;
      left: 50%;
      margin-left: -48px;
    }
    .el-form-item__content {
      position: relative;
      .yanzhen {
        width: 264px;
      }
      img {
        position: absolute;
        width: 130px;
        height: 40px;
        left: 312px;
        top: 0px;

        width: 30%;
      }
    }
    .el-button {
      width: 378px;
      height: 50px;
    }
  }
}
</style>
